package edu.ufrj.pesc.pdfsearch;

import java.io.File;
import java.util.Scanner;
import java.util.Vector;

import edu.ufrj.pesc.pdfsearch.PDFSearchParser.ParsedPDF;

public class PDFSearch {
	
	/**
	 * execute the program
	 **/
	public static void main(String[] args) {
		long startTime;
		
		//call Crawler
		Scanner console = new Scanner(System.in);
        System.out.print("Directory to crawl? ");
        String fs = console.nextLine();
        
    	File f = new File(fs); //File to be crawled
        Vector<File> pdfs = new Vector<File>(); //PDF Vector to be parsed
        PDFSearchCrawler crawler = new PDFSearchCrawler();
        pdfs = crawler.crawl(f, pdfs);
        System.out.println("Number of PDFs to add to index: "+pdfs.size());
        
        //Connect to Server and Clean Index
        PDFSearchSolr solr = new PDFSearchSolr();
		System.out.println("Solr Index cleaned.");
		
		//Parse PDF Vector
		PDFSearchParser parser = new PDFSearchParser();
		int docId = 0;
		startTime = System.currentTimeMillis();
		for (File pdf:pdfs) {
			System.out.println("Processing "+pdf.getName()+" ...");
			ParsedPDF ppdf = parser.processFile(pdf);
			solr.addDocumentToVector(docId, pdf, ppdf.getMd(), ppdf.getContent(), ppdf.getIsbn());
			docId++;
			System.out.println("Processed "+pdf.getName()+" succesfully");
		}
		System.out.println("Parsing time: "+(System.currentTimeMillis()-startTime)/1000+" seconds");
		
		//Upload to Server
		startTime = System.currentTimeMillis();
		System.out.println("Uploading to Sorl...");
		solr.uploadDocuments();
	    System.out.println("Upload to Solr time: "+(System.currentTimeMillis()-startTime)/1000+" seconds");
		System.out.println("Documents added to Sorl!");
		
		console.close();
	}
}
